package com.example.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;

@Slf4j
@Component
public class CaptchaGenerator {

    public static void main(Integer width,Integer height) throws IOException {
        log.info("生成图片验证码宽{}高{}",width,height);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bufferedImage.createGraphics();
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);
        g2d.setFont(new Font("Arial", Font.BOLD, 20));
        String captcha = "abcd";
       /* for (int i = 0; i < 5; i++) {
            char c = (char) (random.nextInt(26) + 'A');
            captcha += c;
            g2d.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
            g2d.drawString(String.valueOf(c), 20 * i + 10, 25);
        }*/
        g2d.dispose();
        ImageIO.write(bufferedImage, "png", new File("captcha.png"));
        System.out.println("Captcha generated: " + captcha);
    }
}
